package com.wuge.gulimall.product.service.impl;

import com.wuge.gulimall.product.dao.AttrDao;
import com.wuge.gulimall.product.dao.AttrGroupDao;
import com.wuge.gulimall.product.entity.AttrEntity;
import com.wuge.gulimall.product.entity.AttrGroupEntity;
import com.wuge.gulimall.product.vo.AttrGroupRelactionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wuge.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wuge.gulimall.product.service.AttrAttrgroupRelationService;

import javax.annotation.Resource;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {
    @Resource
    AttrGroupDao attrGroupDao;
    @Resource
    AttrDao attrDao;

    @Override
    public void update(List<AttrGroupRelactionVo> attrGroupRelactionVo) {
        List<AttrAttrgroupRelationEntity> collect = attrGroupRelactionVo.stream().map((vo) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vo, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        saveBatch(collect);
    }

    @Override
    public PageUtils getNoAttr(Long cateId,Map map) {


        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(cateId);
        Long catelogId = attrGroupEntity.getCatelogId();
        List<AttrGroupEntity> catelog_id = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).eq("attr_type",1);

            List<Long> attrGroups = catelog_id.stream().map((entity) -> {
                return entity.getAttrGroupId();
            }).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroups));
        if (attrAttrgroupRelationEntities.size()!=0){
            List<Long> collect = attrAttrgroupRelationEntities.stream().map((entity) -> {
                return entity.getAttrId();
            }).collect(Collectors.toList());
            queryWrapper.notIn("attr_id",collect);
        }
        IPage iPage = attrDao.selectPage(new Query<AttrEntity>().getPage(map),
                queryWrapper);


        return new PageUtils(iPage);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void deleteRelation(AttrGroupRelactionVo[] attrGroupRelactionVo) {
        getBaseMapper().deleteRelation(attrGroupRelactionVo);
    }

    @Override
    public List<Long> getAttrIds(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attr_id = list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_Group_id", attrGroupId));
        List<Long> collect = attr_id.stream().map((entity) -> {
            return entity.getAttrId();
        }).collect(Collectors.toList());


        return collect;
    }
}